package com.jy.appmarket.widget.entertainment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.RemoteViews;

import com.jy.appmarket.R;
import com.jy.appmarket.activity.WindowActivity;
import com.jy.appmarket.statistics.event.widgetDisableEvent;
import com.jy.appmarket.statistics.event.widgetEnableEvent;
import com.jy.appmarket.statistics.umeng.myUmengStaticstics;
import com.jy.appmarket.utils.LogUtil;
import com.jy.appmarket.utils.WidgetIconUtils;
import com.jy.appmarket.utils.WidgetProviderTools;
import com.rad.statistics.android.StatisticsAgent;



public class EntertainmentWidgetProvider extends AppWidgetProvider{
	public final static String TAG = "EntertainmentWidgetProvider";
	private final static String NAME = "entertainment_widget_start_state";

	private final static String WIDGET_TYPE = "widget_Entertainment";
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		LogUtil.d(TAG, "update");
		Bitmap bmp = null;
		try {
			File file = WidgetIconUtils.getInstance().getIconFile(context,
					WidgetIconUtils.TYPE_ENTERTAINMENT_WIDGET);
			bmp = BitmapFactory.decodeStream(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.widget_icon_default);
			e.printStackTrace();
		}
		RemoteViews rvs = new RemoteViews(context.getPackageName(),
				R.layout.appmarket_widget_entertainment_layout);
		rvs.setTextViewText(R.id.title, context.getString(R.string.entertainment));
		rvs.setImageViewBitmap(R.id.img, bmp);

		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		Intent intent = new Intent(context, WindowActivity.class);
		intent.putExtra(WindowActivity.TYPE, WidgetIconUtils.TYPE_ENTERTAINMENT_WIDGET);
		rvs.setOnClickPendingIntent(R.id.img, PendingIntent.getActivity(context,
				tag, intent, PendingIntent.FLAG_UPDATE_CURRENT));
		appWidgetManager.updateAppWidget(appWidgetIds, rvs);
	}

	// 第一个widget被创建时调用
	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		Log.d(TAG, "onEnabled");
		if (!WidgetProviderTools.isFirstStart(context, NAME, WidgetProviderTools.KEY)) {
			LogUtil.i(TAG, "entertainment widget first start");
			StatisticsAgent.onEvent(context, new widgetEnableEvent(WIDGET_TYPE));
			WidgetProviderTools.saveFirstStartedState(context, NAME, WidgetProviderTools.KEY);
		}
		myUmengStaticstics.onWidgetEnable(context, WIDGET_TYPE);
	}

	// 最后一个widget被删除时调用
	@Override
	public void onDisabled(Context context) {
		Log.d(TAG, "onDisabled");
		super.onDisabled(context);
		StatisticsAgent.onEvent(context, new widgetDisableEvent(WIDGET_TYPE));
		myUmengStaticstics.onWidgetDisable(context, WIDGET_TYPE);
	}
}